; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -passes='loop-mssa(indvars,indvars)' -S < %s | FileCheck %s

target triple = "x86_64-unknown-linux-gnu"

@c = external global i16, align 1
@a = external global i16, align 1

; When we delete %c.promoted, we need to ensure we clear the getSCEVAtScope
; cache or we'll crash trying to access loop disposition of the exit value
; for the remaining IV.
define void @f() {
; CHECK-LABEL: @f(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    br label [[FOR_COND:%.*]]
; CHECK:       for.cond:
; CHECK-NEXT:    br i1 false, label [[FOR_BODY:%.*]], label [[FOR_END4:%.*]]
; CHECK:       for.body:
; CHECK-NEXT:    br label [[FOR_BODY2:%.*]]
; CHECK:       for.body2:
; CHECK-NEXT:    [[INC2:%.*]] = phi i16 [ undef, [[FOR_BODY]] ], [ [[INC:%.*]], [[FOR_BODY2]] ]
; CHECK-NEXT:    [[INC]] = add nuw nsw i16 [[INC2]], 1
; CHECK-NEXT:    store i16 [[INC]], ptr undef, align 1
; CHECK-NEXT:    br i1 true, label [[FOR_BODY2]], label [[CRIT_EDGE:%.*]]
; CHECK:       crit_edge:
; CHECK-NEXT:    [[INC_LCSSA:%.*]] = phi i16 [ [[INC]], [[FOR_BODY2]] ]
; CHECK-NEXT:    store i16 [[INC_LCSSA]], ptr @a, align 1
; CHECK-NEXT:    unreachable
; CHECK:       for.end4:
; CHECK-NEXT:    ret void
;
entry:
  br label %for.cond

for.cond:                                         ; preds = %entry
  br i1 false, label %for.body, label %for.end4

for.body:                                         ; preds = %for.cond
  %c.promoted = load i16, ptr @c, align 1
  br label %for.body2

for.body2:                                        ; preds = %for.body2, %for.body
  %inc33 = phi i16 [ %c.promoted, %for.body ], [ %inc3, %for.body2 ]
  %inc2 = phi i16 [ undef, %for.body ], [ %inc, %for.body2 ]
  %inc = add nsw i16 %inc2, 1
  store i16 %inc, ptr undef, align 1
  %inc3 = add nsw i16 %inc33, 1
  %tobool = icmp ne i16 %inc3, 0
  br i1 %tobool, label %for.body2, label %crit_edge

crit_edge:                                        ; preds = %for.body2
  %inc.lcssa = phi i16 [ %inc, %for.body2 ]
  store i16 %inc.lcssa, ptr @a, align 1
  unreachable

for.end4:                                         ; preds = %for.cond
  ret void
}
